package club.haomoon.core.plugin.notice;

import club.haomoon.core.cache.SpringUtils;
import club.haomoon.core.entity.system.NoticeSettingData;
import club.haomoon.core.exception.OperateException;
import club.haomoon.core.mapper.system.NoticeSettingDataMapper;
import club.haomoon.core.plugin.notice.engine.SmsNoticeHandle;
import club.haomoon.core.plugin.notice.template.SmsTemplate;
import club.haomoon.core.plugin.notice.vo.NoticeSmsVo;
import club.haomoon.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * -------------------------
 * @Description: 通知驱动
 * @Author: Wangjianwei
 * @Date: 2025/02/13 15:48
 * -------------------------
 */
public class NoticeDriver {

    public static void handle(NoticeSmsVo noticeSmsVo) {
        // 场景模板
        NoticeSettingDataMapper noticeSettingMapper = SpringUtils.getBean(NoticeSettingDataMapper.class);
        NoticeSettingData noticeSetting = noticeSettingMapper.selectOne(
                new QueryWrapper<NoticeSettingData>()
                        .eq("scene_id", noticeSmsVo.getScene())
                        .last("limit 1"));

        if (StringUtils.isNull(noticeSetting)) {
            throw new OperateException("消息场景不存在!");
        }

        // 短信通知
        SmsTemplate smsTemplate = new SmsTemplate();
        smsTemplate.setName(noticeSetting.getSceneName());
        smsTemplate.setType(noticeSetting.getType());
        smsTemplate.setParams(noticeSetting.getSmsNotice());
        if (StringUtils.isNotNull(smsTemplate.getStatus()) && smsTemplate.getStatus().equals(1)) {
            (new SmsNoticeHandle()).send(noticeSmsVo, smsTemplate);
        }
    }

}
